package com.cw.controller;

import com.cw.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@Slf4j
public class UploadController {
    @PostMapping("/upload")
    public Result upload(String username, Integer age, MultipartFile image) throws IOException {
        log.info("文件上传：{},{},{}",username,age,image);
        //讲文件存储在服务器的磁盘目录中 C:\Users\JustinWen\Desktop

        //获取文件原始名
        String originalFilename = image.getOriginalFilename();

        //构造唯一的文件名(不可重复) -uuid(通用唯一识别码)
        assert originalFilename != null; //断言语句,用于确保变量不为null
        int index = originalFilename.lastIndexOf(".");
        String s = originalFilename.substring(index);
        String newName = UUID.randomUUID().toString()+s;
        image.transferTo(new File("C:\\Users\\JustinWen\\Desktop\\"+newName));
        return Result.success();
    }
}
